package presentation.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.JTable;

import usecasecontrol.ManageProductsController;
import business.exceptions.BackendException;
import business.exceptions.UIException;
import business.externalinterfaces.Product;

public class ManageProductsData {
	
	ManageProductsController usecaseControl = new ManageProductsController();
	
	// ///// make this class a singleton
	private static final ManageProductsData instance = new ManageProductsData();

	public static ManageProductsData getInstance() {
		return instance;
	}
	
	public List<String[]> getProductsRelatedToCatalog(String catalog) throws BackendException {
		List<Product> prods = usecaseControl.getProductsList(catalog);
		return business.util.ProductUtil.extractProductInfoForManager(prods);
	}
	
	public List<String[]> getCatalogs() throws BackendException {
		List<String> catalogs = usecaseControl.getCatalogList();
		List<String[]> namesAsStringArrays = new ArrayList<String[]>();
		for(String n : catalogs) {
			String[] next = {n};
			namesAsStringArrays.add(next);
		}
		return namesAsStringArrays;
	}
	
	public Properties getProductFieldValues(JTable table) throws UIException {
		int selectedRow = table.getSelectedRow();
    	if(selectedRow >= 0) {
    		Properties productInfo = new Properties();
    		for(ProdFieldName fld : ProdFieldName.values()) {
    			productInfo.setProperty(fld.getFieldName(), 
    					(String)table.getValueAt(selectedRow, fld.getIndex()));
    		}
    		return productInfo;
    		
    		/*
    		 * String[] fldNames = DefaultData.FIELD_NAMES;
    		//index for Product Name
    		int columnIndex = DefaultData.PRODUCT_NAME_INT;
    		productInfo.setProperty(fldNames[columnIndex],
    								(String)model.getValueAt(selectedRow,columnIndex));
    								
     		//index for Price Per Unit
    		columnIndex = DefaultData.PRICE_PER_UNIT_INT;
    		productInfo.setProperty(fldNames[columnIndex],
    								(String)model.getValueAt(selectedRow,columnIndex));
    								       								
    		//index for Mfg Date
    		columnIndex = DefaultData.MFG_DATE_INT;
    		productInfo.setProperty(fldNames[columnIndex],
    								(String)model.getValueAt(selectedRow,columnIndex));
    								        								       								
    		//index for Quantity						
    		columnIndex = DefaultData.QUANTITY_INT;					
     		productInfo.setProperty(fldNames[columnIndex],
    								(String)model.getValueAt(selectedRow,columnIndex));
    		*/					       														
    		
    		
    		
    	}
    	else {
   			throw new UIException(MessagesUtil.SELECT_ROW_MSG);
    		
    	}
	}

	private ManageProductsData() {
	}
	
	 private enum ProdFieldName {
		    //Name on UI, position in UI
	    	NAME("Product Name", 0), 
	    	PRICE("Price Per Unit", 1), 
	    	MF_DATE("Mfg. Date", 2), 
	    	QUANTITY("Quantity", 3);
	    	
	    	int index;
	    	String fieldName;
	    	ProdFieldName(String fieldName, int index) {
	    		this.index = index;
	    		this.fieldName = fieldName;
	    	}
	    	public int getIndex() {
	    		return index;
	    	}
	    	public String getFieldName() {
	    		return fieldName;
	    	}
	    }
}
